package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.b.sky.mes.em.model.SpotInspection;
import cn.b.sky.mes.em.model.SpotInspectionAttach;
import cn.b.sky.mes.em.model.condition.SpotInspectionCondition;
import cn.b.sky.mes.em.service.SpotInspectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.model.Attachment;
import cn.b.sky.user.service.AttachmentService;

/**
 * <strong>Title : SpotInspectionController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备点检控制器<br> 
 * <strong>Create on : 2015年11月16日 上午10:49:34<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/spotInspection/*")
public class SpotInspectionController extends SecurityController {

  @Resource
  SpotInspectionService spotInspectionService;

  @Resource
  AttachmentService attachmentService;

  @RequestMapping("spotInspection_list")
  public String selectSpotInspection(@ModelAttribute("condition") SpotInspectionCondition condition, String navTabId,
                                     Model model) {
    condition.setSiteCode(getSiteCode());
    List<SpotInspection> list = spotInspectionService.selectSpotInspectionListByCondition(condition);
    int count = spotInspectionService.selectSpotInspectionCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "em/spotInspection/spotInspection_list";
  }

  @RequestMapping("addSpotInspection")
  public String addSpotInspection(String navTabId, Model model, HttpServletRequest request) {
    model.addAttribute("jsessionId", request.getSession().getId());
    return "em/spotInspection/spotInspection_add";
  }

  @RequestMapping("modifySpotInspection")
  public String modifySpotInspection(String navTabId, Model model, String id) {
    SpotInspection spotInspection = spotInspectionService.getSpotInspection(Long.valueOf(id));
    if (!StringUtils.isBlank(spotInspection.getAttachmentCode())) {
      Attachment attachmentFile = attachmentService.selectAttachmentByAttachmentCode(getSiteCode(),
          spotInspection.getAttachmentCode());
      model.addAttribute("attachmentFile", attachmentFile);
    }
    model.addAttribute("spotInspection", spotInspection);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "em/spotInspection/spotInspection_edit";
  }

  @RequestMapping("viewSpotInspection")
  public String viewSpotInspection(String navTabId, Model model, String id) {
    SpotInspection spotInspection = spotInspectionService.getSpotInspection(Long.valueOf(id));
    if (!StringUtils.isBlank(spotInspection.getAttachmentCode())) {
      Attachment attachment = attachmentService.selectAttachmentByAttachmentCode(getSiteCode(),
          spotInspection.getAttachmentCode());
      model.addAttribute("attachmentFile", attachment);
    }
    model.addAttribute("spotInspection", spotInspection);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "read");
    return "em/spotInspection/spotInspection_view";
  }

  @RequestMapping("saveSpotInspection")
  @ResponseBody
  public DWZResult saveSpotInspection(HttpServletRequest request, String id, SpotInspection inspection,
      HttpServletResponse response) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
    inspection.setSiteCode(getSiteCode());
    setCreateInfo(inspection);
    if (file.getSize() != 0) {
      SpotInspectionAttach attach = new SpotInspectionAttach();
      if (StringUtils.isBlank(id)) {
        spotInspectionService.insertSpotInspectionAndAttach(inspection, attach, file, getSiteCode());
      } else {
        spotInspectionService.updateSpotInspectionAndAttach(inspection, attach, file, getSiteCode());
      }
    } else {
      if (StringUtils.isBlank(id)) {
        spotInspectionService.insertSpotInspection(inspection);
      } else {
        spotInspectionService.updateSpotInspection(inspection);
      }
    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null, null, null, null);
  }

  @RequestMapping("deleteSpotInspection")
  @ResponseBody
  public DWZResult deleteRunningRecord(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] idArry = ids.split(",");
    int len = idArry.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = spotInspectionService.deleteSpotInspection(Long.valueOf(idArry[i]), getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("deleteAttachment")
  @ResponseBody
  public DWZResult deleteAttachment(HttpServletRequest request, String navTabId, String attachmentCode) {
    spotInspectionService.deleteAttachment(SecurityContextUtil.getSiteCode(), attachmentCode);
    return ajaxDone(null);
  }

}
